package com.ruoyi.insert.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.profession.domain.Papers;
import com.ruoyi.profession.mapper.PapersMapper;
import com.ruoyi.profession.service.IPapersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 论文信息Controller
 * 
 * @author 靳双伟
 * @date 2024-06-01
 */
@RestController
@RequestMapping("/insert/papers")
public class IPapersController extends BaseController
{
    @Autowired
    private IPapersService papersService;

    @Autowired
    private PapersMapper papersMapper;

    /**
     * 查询论文信息列表
     */
    @PreAuthorize("@ss.hasPermi('profession:papers:list')")
    @GetMapping("/list")
    public TableDataInfo list(Papers papers)
    {
        startPage();
        List<Papers> list = papersService.selectPapersList(papers);
        return getDataTable(list);
    }

    /**
     * 导出论文信息列表
     */
    @PreAuthorize("@ss.hasPermi('profession:papers:export')")
    @Log(title = "论文信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Papers papers)
    {
        List<Papers> list = papersService.selectPapersList(papers);
        ExcelUtil<Papers> util = new ExcelUtil<Papers>(Papers.class);
        util.exportExcel(response, list, "论文信息数据");
    }

    /**
     * 获取论文信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('profession:papers:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(papersService.selectPapersById(id));
    }

    /**
     * 新增论文信息
     */
    @PreAuthorize("@ss.hasPermi('profession:papers:add')")
    @Log(title = "论文信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Papers papers)
    {

        Long basicId = papers.getBasicInfoId();
        QueryWrapper<Papers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("basic_info_id", basicId);
        if (papersMapper.selectCount(queryWrapper)<20){
            return toAjax(papersService.insertPapers(papers));
        }
        return AjaxResult.countGt();
    }

    /**
     * 修改论文信息
     */
    @PreAuthorize("@ss.hasPermi('profession:papers:edit')")
    @Log(title = "论文信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Papers papers)
    {
        return toAjax(papersService.updatePapers(papers));
    }

    /**
     * 删除论文信息
     */
    @PreAuthorize("@ss.hasPermi('profession:papers:remove')")
    @Log(title = "论文信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(papersService.deletePapersByIds(ids));
    }
}
